import Router from 'koa-router'
import doubanData from '../../static/data.json'
import { myTools } from '../../assets/myTools/jsTools/jsTools'
// import User from '../dbs/module/user'

const router = new Router({ prefix: '/goods' })


router.get('/test', async (ctx) => {
  ctx.body = {
    code: 0
  }
})

router.post('/category', async (ctx) => {
  let categoryGoods = []
  let {params: {category, start, count}} = ctx.request.body
  doubanData.forEach((item, idx) => {
    if(category === 'all') {
      categoryGoods.push({
        img: item.photos.defaultimg,
        product_id: item.product_id,
        title: item.title,
        desc: item.desc,
        price: item.price,
        promote_price: item.promote_price
      })
    }else{
      if (item.category.join().indexOf(category) !== -1){
        categoryGoods.push({
          img: item.photos.defaultimg,
          product_id: item.product_id,
          title: item.title,
          desc: item.desc,
          price: item.price,
          promote_price: item.promote_price
        })
      }
    }
  })
  ctx.body = {
    code: 0,
    data: categoryGoods.slice(start, start + count)
  }
})

router.post('/prodetail', async (ctx) => {
  let {params: {pid}} = ctx.request.body
  let idx = myTools.arrSomeItem(doubanData, 'product_id', pid)
  let item = doubanData[idx]
  let proDetail = item.m_detail
  proDetail.head_img = item.head_img.mobile
  proDetail.title = item.title
  proDetail.skus = item.skus
  proDetail.reviews = item.p_detail.reviews
  proDetail.sale_sort = item.sale_sort
  proDetail.price_info = item.price_info

  ctx.body = {
    code: 0,
    data: proDetail
  }
})

router.post('/skuDetail', async (ctx) => {
  let {params: {skuid}} = ctx.request.body
  let skuData = {}
  doubanData.some(i => {
    let idx = myTools.arrSomeItem(i.skus, 'id', skuid)
    if(idx !== false) skuData = i.skus[idx]
    return idx === false ? false : true
  })
  ctx.body = {
    code: 0,
    data: skuData
  }
})

router.post('/skusDetails', async (ctx) => {
  let {params: {skuids}} = ctx.request.body
  let skusDatas = []

  skuids.forEach(i => {
    doubanData.some(j => {
      let idx = myTools.arrSomeItem(j.skus, 'id', i)
      if(idx !== false) {skusDatas.push(j.skus[idx])}
      return idx === false ? false : true
    })
  })

  ctx.body = {
    code: 0,
    data: skusDatas
  }
})
export default router
